﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Noftware.SPArchiveTweets
{
    /// <summary>
    /// SPPersistedObject to hold timer settings.
    /// </summary>
    [System.Runtime.InteropServices.Guid("4B492AA7-A7D4-4F2B-B234-46B937E3B275")]
    public class AppData : SPPersistedObject
    {
        [Persisted]
        public Dictionary<string, string> Entries;

        public AppData() { }
        public AppData(string Name, SPPersistedObject Parent, Guid Id) : base(Name, Parent, Id) { }

        /// <summary>
        /// Unique Id of the SPPersistedObject.
        /// </summary>
        private const string DATA_TAG = "4B492AA7-A7D4-4F2B-B234-46B937E3B275";

        /// <summary>
        /// URL of the SharePoint in which to create the Twitter lists.
        /// </summary>
        public const string SHAREPOINT_SITE_URL = "SharePointSiteUrl";

        // Selected value of the timer frequency
        public const string SELECTED_VALUE = "SelectedValue";

        // An enabled per item as per count in SCHEDULE_COUNT
        public const string SCHEDULE_ENABLED = "ScheduleEnabled_";

        // An hour per item as per count in SCHEDULE_COUNT
        public const string SCHEDULE_HOUR_TIME = "ScheduleHour_";

        // OAuth consumer key setting
        public const string OAUTH_CONSUMER_KEY = "OAuthConsumerKey";

        // OAuth consumer secret setting
        public const string OAUTH_CONSUMER_SECRET = "OAuthConsumerSecret";

        // OAuth access token setting
        public const string OAUTH_ACCESS_TOKEN = "OAuthAccessToken";

        // OAuth access token secret setting
        public const string OAUTH_ACCESS_TOKEN_SECRET = "OAuthAccessTokenSecret";

        // Timeline include re-tweets setting
        public const string INCLUDE_RETWEETS = "IncludeRetweets";

        // Timeline include entities setting
        public const string INCLUDE_ENTITIES = "IncludeEntities";

        // Timeline maximum tweets to fetch setting
        public const string TWEET_COUNT = "TweetCount";

        private static AppData local;
        public static AppData Local
        {
            get
            {
                {
                    var farm = SPFarm.Local;
                    local = farm.GetChild<AppData>(DATA_TAG);

                    if (local == null)
                    {
                        Guid id = Guid.NewGuid();
                        SPPersistedObject parentWebApp = (SPPersistedObject)farm;
                        local = new AppData(DATA_TAG, parentWebApp, id);
                        local.Update();
                    }

                    if (local.Entries == null)
                    {
                        local.Entries = new Dictionary<string, string>();

                        // Defaults
                        local.Entries.Add(AppData.SHAREPOINT_SITE_URL, "http://");
                        local.Entries.Add(AppData.OAUTH_ACCESS_TOKEN, string.Empty);
                        local.Entries.Add(AppData.OAUTH_ACCESS_TOKEN_SECRET, string.Empty);
                        local.Entries.Add(AppData.OAUTH_CONSUMER_KEY, string.Empty);
                        local.Entries.Add(AppData.OAUTH_CONSUMER_SECRET, string.Empty);
                        local.Entries.Add(AppData.SELECTED_VALUE, string.Empty);
                        local.Entries.Add(AppData.INCLUDE_ENTITIES, "true");
                        local.Entries.Add(AppData.INCLUDE_RETWEETS, "true");
                        local.Entries.Add(AppData.TWEET_COUNT, "50");
                        local.Update();
                    }
                }
                return local;
            }
        }
    }
}
